package dx.util;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

/**
 * 随机数相关的工具类
 *@classname: RandomUtil
 *@author: duanx
 *@date: 2021/1/30 23:08
 **/
public class RandomUtil {
    private static Logger logger = LoggerFactory.getLogger(RandomUtil.class);

    /**
     * 获取一个size大小的从min-max范围的随机数组
     * @param size 大小
     * @param min 最小
     * @param max 最大
     * @return 随机数组
     */
    @Nullable
    public static int[] getRandomArray(int size, int min, int max){
        if (size==0||max-min<size){
            logger.error("获取"+size+"位的，["+min+","+max+"]区间的数组成功！");
            return null;
        }
        int [] array = new int[size];
        for (int i= 0;i<size;i++){
            int temp = min +(int) (Math.random()*(max-min));
            array[i] = temp;
        }
        logger.info("获取"+size+"位的，["+min+","+max+"]区间的数组成功！");
        return array;
    }

    /**
     * 产生n位随机数
     * @param n 位数[1,15]
     * @return 随机数
     */
    @NotNull
    public static String getRandom(int n){
        int max = 18;
        int min = 1;
        if (n<min||n>max){
            logger.error("参数不正确");
            throw  new IllegalArgumentException("参数范围[1,15],整数");
        }
        return String.valueOf((int) (Math.random()*9*Math.pow(10,n-1)) + (long)Math.pow(10,n-1));
    }

    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        String id = "422126199504224017";
        int digits = 18;
        int count = 0;
        while (true){
            String string = getRandom(digits);
            logger.debug(string);
            if (id.equals(string)){
                strings.add(string);
                break;
            }
            count++;
        }
        System.out.println(count+strings.size());
        int [] test = getRandomArray(10,0,10);
        if (test!=null){
            for (int value : test) {
                logger.debug(String.valueOf(value));
            }
        }
    }
}
